package ma.ensao.evalmetrics.controller.metrics;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import ma.ensao.evalmetrics.model.D;

import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaSource;

/*
 * M�trique d'Abstraction
 * Abstraction A : Le rapport entre le nombre de classes abstraites et le nombre total de classes dans un package
 */
public class AbstractionManager {
	D d = new D();
	int resultat = 0;
	int absClasses;
	int nonAbs;
	double p = 0;
	
	
	public int getNonAbs() {
		return nonAbs;
	}

	public void setNonAbs(int nonAbs) {
		this.nonAbs = nonAbs;
	}

	public int getAbsClasses() {
		return absClasses;
	}

	public void setAbsClasses(int absClasses) {
		this.absClasses = absClasses;
	}

	public double getP() {
		return p;
	}

	public void setP(double p) {
		this.p = p;
	}

	// Parcourir le fichier pour extraire les classes (.java)
	public void abs(File file) {
		absClasses=0;
		nonAbs=0;
		JavaDocBuilder builder = new JavaDocBuilder();
		try {

			builder.addSource(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		JavaSource src = builder.getSources()[0];
		System.out.println("java classe " + src.getClasses().length);
		for (int i = 0; i < src.getClasses().length; i++) {
			JavaClass cls = src.getClasses()[i];
			System.out.println(cls.getPackage().getName());

			if (cls.isAbstract() == true) {

				absClasses++;
			} else {

				nonAbs++;

			}

		}

		p = (double) absClasses / (nonAbs + absClasses);
		
		d.setAbs(p);

	}

	public int getResultat() {
		return resultat;
	}

	public void setResultat(int resultat) {
		this.resultat = resultat;
	}

	public static void main(String[] args) {
		AbstractionManager ab = new AbstractionManager();
		File file = new File(
				"C:/Users/sam/Desktop/RatRoidClient/src/com/hicham/ratroid/A.java");
		ab.abs(file);
	}

}
